var express = require('express');

const xlsx = require("node-xlsx").default;
var router = express.Router();
const { DishModel,DishPlanModel,DishTypeModel} = require('../mongodb/db')
/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});
router.get('/list',async (req,res)=>{

  let data = await DishModel.find().populate('type')
  res.send({
    code:200,
    msg:'成功',
    data
  })
})

router.get('/planList',async (req,res)=>{
  let data = await DishPlanModel.find()
  res.send({
    code:200,
    msg:'成功',
    data
  })
})

router.get('/typeList',async (req,res)=>{
  let data = await DishTypeModel.find()
  res.send({
    code:200,
    msg:'成功',
    data
  })
})

router.post('/planAdd',async (req,res)=>{
  console.log(req.body);
  await DishPlanModel.create(req.body)
  res.send({
    code:200,
    msg:'成功'
  })
})


router.post('/put',async(req,res)=>{
  await DishModel.updateMany({_id:req.body._id},{flag:req.body.flag,time:Date.now()})
  res.send({
    code:200,
    msg:'成功'
  })
})

router.get('/count',async(req,res)=>{
  let foodLen = await FoodsModel.find({collect:true}).countDocuments()
  let canteenLen = await CanteenModel.find({collect:true}).countDocuments()
  let localLen = await LocalModel.find({collect:true}).countDocuments()
  res.send({
    code:200,
    msg:'成功',
    data:{foodLen,canteenLen,localLen}
  })
})

router.get('/foodCollect',async(req,res)=>{
  let data = await FoodsModel.find({collect:true}).populate('tid')
  console.log(data)
  res.send({
    code:200,
    msg:'成功',
    data
  })
})
router.get('/canteenCollect',async(req,res)=>{
  let data = await CanteenModel.find({collect:true})
  res.send({
    code:200,
    msg:'成功',
    data
  })
})
router.get('/localCollect',async(req,res)=>{
  let data = await LocalModel.find({collect:true})
  res.send({
    code:200,
    msg:'成功',
    data
  })
})

router.get('/getusers',async(req,res)=>{
  let data = await UsersModel.find()
  res.send({
    code:200,
    data
  })
})

router.post('/updatename',async(req,res)=>{
  console.log(req.body);
  
  await UsersModel.updateOne({_id:req.body._id},{username:req.body.username})
  let data = await UsersModel.findOne({_id:req.body._id})
  console.log(data);
  
  res.send({
    code:200,
    data
  })
})

router.get('/childList',async(req,res)=>{
  let data = await SizeChildModel.find().sort({cSort:-1})
  res.send({
    code:200,
    msg:'成功',
    data
  })
})

router.post('/childAdd',async(req,res)=>{
  await SizeChildModel.create(req.body)
  res.send({
    code:200,
    msg:'成功'
  })
})

router.post('/childEdit',async (req,res)=>{
  await SizeChildModel.updateOne({_id:req.body._id},req.body)
  res.send({
    code:200,
    msg:'成功'
  })
})


router.post('/childDel',async (req,res)=>{
  let {_id} =  req.body
  console.log(_id);
  
  // await SizeModel.deleteMany({_id})
  // await ImageModel.deleteMany({xid:_id})
  await SizeChildModel.deleteMany({_id})
  res.send({
    code:200,
    msg:'成功',
  })
})
router.post('/del',async (req,res)=>{
  let {_id} =  req.body
  
  await DishModel.deleteMany({_id})
  // await ImageModel.deleteMany({xid:_id})
  // await SizeChildModel.deleteMany({_id})
  res.send({
    code:200,
    msg:'成功',
  })
})

router.post('/add',async (req,res)=>{
  console.log(req.body);
  await DishModel.create(req.body)
  res.send({
    code:200,
    msg:'成功'
  })
})

router.post('/edit',async (req,res)=>{

  await SizeModel.updateOne({_id:req.body._id},req.body)
  res.send({
    code:200,
    msg:'成功',
  })
})

router.get('/xc',async (req,res)=>{
  XcModel.aggregate([
        {
          $lookup: {
            from: 'image', 
            localField: '_id', 
            foreignField: 'xid', 
            as: 'num' 
          }
        }
    ]).then(a=>{
        res.send({
          code:200,
          msg:'成功',
          data:a
        })
    })
 
})





const multiparty = require('multiparty')
const fs = require('fs')
// router.post('/file',async (req,res)=>{
//   const form = new multiparty.Form()
//   form.uploadDir = 'files'
//   form.parse(req,(error,fields,files)=>{
//   const dataXlsx = xlsx.parse(fs.readFileSync(files.file[0].path))
//   let datas = dataXlsx[0].data
//   const headers = datas[0]
//   datas.splice(0,1)
//   let data = []
//   datas.forEach(row => {
//     let entry = {};
//     headers.forEach((header, index) => {
//         entry[header] = row[index]; 
//     });
//     data.push(entry);
//   });
//   console.log(data);
//    res.send({
//     code:200,
//     msg:'成功',
//     fileUrl:'http://localhost:3000/'+files.file[0].path
//   })
//   })
// })


router.post('/image',async (req,res)=>{
  const form = new multiparty.Form()
  form.uploadDir = 'upload'
  form.parse(req,(error,fields,files)=>{
    res.send({
          code:200,
          msg:'成功',
          fileUrl:'http://localhost:3000/'+files.file[0].path
        })
  }
)
})
 



module.exports = router;